#include <opencv2/objdetect.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

#include "mock_detect.h"

using namespace std;
using namespace cv;

/** Global variables */
CascadeClassifier face_cascade;

int initDetect(const std::string &haarcascadePath)
{
    //-- 1. Load the cascades
    if (!face_cascade.load(haarcascadePath))
    {
        cout << "--(!)Error loading face cascade\n";
        return -1;
    };

    return 0;
}

int detect(cv::Mat frame, DetectBBox_t &bboxs, uint32_t index)
{
    cv::Mat frame_gray;
    cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
    equalizeHist(frame_gray, frame_gray);

    //-- Detect faces
    std::vector<Rect> faces;
    face_cascade.detectMultiScale(frame_gray, faces);
    if (faces.size() > 0)
    {
        bboxs.index = index;
        for (size_t i = 0; i < faces.size(); i++)
        {
            DetectRect_t rect;
            rect.x = faces[i].x;
            rect.y = faces[i].y;
            rect.h = faces[i].height;
            rect.w = faces[i].width;
            bboxs.rects.push_back(rect);
        }
    }
    return faces.size();
}